package com.jingxing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jingxing.config.ApiResult;
import com.jingxing.config.UserLoginToken;
import com.jingxing.pojo.PlayerUserInfo;
import com.jingxing.pojo.RewardReceive;
import com.jingxing.pojo.SignUpReward;
import com.jingxing.service.PlayerUserInfoService;
import com.jingxing.service.RewardReceiveService;
import com.jingxing.service.SignUpRewardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/signUpReward")
@Api(value = "API", tags = {"奖品相关"})
@UserLoginToken
public class SignUpRewardController {
    @Autowired
    private SignUpRewardService signUpRewardService;

    @Autowired
    private PlayerUserInfoService playerUserInfoService;

    @Autowired
    private RewardReceiveService rewardReceiveService;

    @PostMapping("/saveSignUpReward")
    @ApiOperation("添加奖品及领奖次数")
    @Transactional
    public ApiResult saveSignUpReward(String prizeName, Integer receiveSfrequency, String createBy) {
        if (StringUtils.isBlank(prizeName)) {
            return ApiResult.parameterError("奖品名不能为空");
        }
        if (receiveSfrequency == null) {
            return ApiResult.parameterError("领取次数不能为空");
        }
        if (StringUtils.isBlank(createBy)) {
            return ApiResult.parameterError("创建人ID不能为空");
        }
        SignUpReward signUpReward = new SignUpReward();
        signUpReward.setPrizeName(prizeName);
        signUpReward.setReceiveSfrequency(receiveSfrequency);
        signUpReward.setCreateBy(createBy);
        boolean save = signUpRewardService.save(signUpReward);
        if (save) {
            List<PlayerUserInfo> list = playerUserInfoService.list();
            if (list != null && list.size() > 0) {
                List<RewardReceive> receives = new ArrayList<>();
                for (PlayerUserInfo playerUserInfo: list) {
                    RewardReceive rewardReceive = new RewardReceive();
                    rewardReceive.setPlayerId(playerUserInfo.getId());
                    rewardReceive.setPrizeId(signUpReward.getId());
                    rewardReceive.setReceiveSfrequency(signUpReward.getReceiveSfrequency());
                    rewardReceive.setCreateBy(createBy);
                    receives.add(rewardReceive);
                }
                boolean saveBatch = rewardReceiveService.saveBatch(receives);
                return ApiResult.result(saveBatch);
            }
        }
        return ApiResult.result(save);
    }

    @PostMapping("/getSignUpRewards")
    @ApiOperation("获取奖品列表")
    public ApiResult getSignUpRewards(String prizeName) {
        QueryWrapper<SignUpReward> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(prizeName)) {
            queryWrapper.like("prize_name", prizeName);
        }
        List<SignUpReward> list = signUpRewardService.list(queryWrapper);
        return ApiResult.ok(list);
    }

    @PostMapping("/updateSignUpReward")
    @ApiOperation("修改奖品名称")
    public ApiResult updateSignUpReward(String id, String prizeName, String updateBy) {
        if (StringUtils.isBlank(id)) {
            return ApiResult.parameterError("ID不能为空");
        }
        if (StringUtils.isBlank(prizeName)) {
            return ApiResult.parameterError("奖品名不能为空");
        }
        if (StringUtils.isBlank(updateBy)) {
            return ApiResult.parameterError("修改人ID不能为空");
        }
        SignUpReward signUpReward = new SignUpReward();
        signUpReward.setId(id);
        signUpReward.setPrizeName(prizeName);
        signUpReward.setUpdateBy(updateBy);
        boolean update = signUpRewardService.updateById(signUpReward);
        return ApiResult.result(update);
    }

    @PostMapping("/delectSignUpReward")
    @ApiOperation("删除奖品及奖励领取")
    @Transactional
    public ApiResult delectSignUpReward(String id) {
        if (StringUtils.isBlank(id)) {
            return ApiResult.parameterError("ID不能为空");
        }
        boolean remove = signUpRewardService.removeById(id);
        if (remove) {
            QueryWrapper<RewardReceive> queryWrapper = new QueryWrapper();
            queryWrapper.eq("prize_id", id);
            rewardReceiveService.remove(queryWrapper);
        }
        return ApiResult.result(remove);
    }


}
